package hackthon.service;

import hackthon.core.Constant;
import hackthon.dao.EmailDao;
import hackthon.dao.RegisterDao;
import hackthon.pojo.Email;
import hackthon.pojo.Register;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;

/**
 * Created by wuwenhao on 15/1/8.
 */

@Service
public class RegisterService {

    @Autowired
    public RegisterDao registerDao;

    @Autowired
    public EmailDao emailDao;

    public int updateRegisterStatus(int emailId, String attendEmail,int status){
        return (Integer)registerDao.updateRegisterStatus(emailId,attendEmail,status);
    }

    public void initRegister(int emailId, String attendEmail){
        registerDao.initRegister(emailId,attendEmail);
    }

    public List getCurrentEmailList(String attendEmail){
        return registerDao.getCurrentEmail(attendEmail);

    }

    public int ifNeedWaiting(int emailId){
        List list = registerDao.getListByEmailId(emailId);
        Iterator iterator = list.iterator();
        int i = 0 ;
        while (iterator.hasNext()){
            i++;
            iterator.next();
        }
        Email email = emailDao.getEmailById(emailId);
        int maxCount = email.getAttendMax();
        int maxCountAll = email.getAttendMaxAll();
        if(i<maxCount){
            return Constant.SUCCESS;
        }else if(i<maxCountAll){
            return  Constant.WAITING;
        }else {
            return Constant.REJECTED;
        }
    }

    public Register getByEmailAndAttend(int emailId, String attendEmail){
        return registerDao.getByEmailAndAttend(emailId,attendEmail);
    }
}
